const { choicesList } = require("../config/download");
const { technologyStackChoices, clientChoices } = require("../config");
const inquirer = require("inquirer");
const t = require("../config/dict");

module.exports = (context) => {
  return inquirer
    .prompt([
      {
        name: "projectClient",
        message: t(context.lang, "客户端") + "?",
        default: 0,
        pageSize: 4,
        type: "rawlist",
        choices: [
          ...clientChoices,
          {
            name: t(context.lang, "取消"),
            value: "cancel",
          },
        ],
      },
    ])
    .then((answers) => {
      const projectClient = answers.projectClient;
      if (answers.projectClient === "cancel") {
        return Promise.reject({
          message: [t(context.lang, "用户取消"), t(context.lang, "。")].join(
            ""
          ),
        });
      }
      context = {
        ...context,
        metadata: {
          ...(context.metadata || {}),
          ...answers,
        },
      };
      return technologyStackChoices()
        .then((stackChoices) => {
          let stacks = [];
          for (let n in stackChoices) {
            let item = stackChoices[n];
            if (projectClient === "web") {
              !/uniapp|taro|native/.test(n) &&
                (item.value = n) &&
                stacks.push(item);
            } else {
              /uniapp|taro|native/.test(n) &&
                (item.value = n) &&
                stacks.push(item);
            }
          }
          return inquirer
            .prompt([
              {
                name: "projectTechnologyStack",
                message: t(context.lang, "项目技术栈"),
                default: 0,
                pageSize: stacks.length + 2,
                type: "rawlist",
                choices: [
                  ...stacks,
                  {
                    name: t(context.lang, "取消"),
                    value: "cancel",
                  },
                ],
              },
            ])
            .then((answers) => {
              let projectTechnologyStack = answers.projectTechnologyStack;
              if (answers.projectTechnologyStack === "cancel") {
                return Promise.reject({
                  message: [
                    t(context.lang, "用户取消"),
                    t(context.lang, "。"),
                  ].join(""),
                });
              }
              return choicesList((name, choicesURLs) => {
                if (projectTechnologyStack === name) {
                  return choicesURLs[name];
                }
              })
                .then((filterValue) => {
                  return inquirer
                    .prompt([
                      {
                        name: "projectType",
                        message: t(context.lang, "项目类型"),
                        default: 0,
                        pageSize: filterValue.length + 2,
                        type: "rawlist",
                        choices: [
                          ...filterValue,
                          {
                            name: t(context.lang, "取消"),
                            value: "cancel",
                          },
                        ],
                      },
                    ])
                    .then((answers) => {
                      if (answers.projectType === "cancel") {
                        return Promise.reject({
                          message: [
                            t(context.lang, "用户取消"),
                            t(context.lang, "。"),
                          ].join(""),
                        });
                      }
                      const projectDevelopLang = /Typescript/.test(
                        answers.projectType
                      )
                        ? "typescript"
                        : "javascript";

                      context.metadata.projectType = [answers.projectType];
                      context.metadata.projectDevelopLang = projectDevelopLang;
                      context.metadata.projectTechnologyStack =
                        projectTechnologyStack;
                      context.metadata.projectClient = projectClient;
                      context.metadata.projectTypeConfig =
                        stackChoices[projectTechnologyStack][
                          answers.projectType
                        ];
                      return context;
                    });
                })
                .catch((err) => Promise.reject(err));

              /* return inquirer
          .prompt([
            {
              name: "projectDevelopLang",
              message: [t(context.lang, "开发语言"), "?"].join(""),
              default: "javascript",
              pageSize: 4,
              type: "rawlist",
              choices: [
                ...developLangChoices,
                {
                  name: t(context.lang, "取消"),
                  value: "cancel",
                },
              ],
            },
          ])
          .then((result) => {
            let projectDevelopLang = result.projectDevelopLang;
            if (answers.projectDevelopLang === "cancel") {
              return Promise.reject({
                message: [
                  t(context.lang, "用户取消"),
                  t(context.lang, "。"),
                ].join(""),
              });
            } */

              //});
            });
        })
        .catch((err) => Promise.reject(err));
    });
};
